package com.sfx.concurrent.demo.concurrent.TestWaitNotify;

import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-06
 * Time: 18:30
 */

@Slf4j(topic = "c.TestDemo1")
public class TestDemo1 {
    private static final Object obj = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            synchronized (obj) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("执行其他代码...");
            }
        },"t1");
        Thread t2 = new Thread(()->{
            synchronized (obj) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("执行其他代码...");
            }
        },"t2");
        t1.start();
        t2.start();
        //1s之后主线程唤醒在WaitSet中的所有线程
        Thread.sleep(1000);
        synchronized (obj) {
            obj.notifyAll();
        }
    }
}